package com.gph.saviorframework.mq.api.bean;

import com.gph.saviorframework.mq.api.MQFactory;
import com.gph.saviorframework.mq.api.Message;
import com.gph.saviorframework.mq.api.Producer;
import com.gph.saviorframework.mq.api.SendResult;
import com.gph.saviorframework.mq.api.exception.MQClientException;

import java.util.Properties;

/**
 * Created by root on 16-7-10.
 */
public class ProducerBean implements Producer{

    private Properties properties;

    private Producer producer;

    /**
     * 启动服务
     */
    public void start() {
        if (null == this.properties) {
            throw new MQClientException("properties not set");
        }

        this.producer = MQFactory.createProducer(this.properties);
        this.producer.start();
    }

    /**
     * 关闭服务
     */
    public void shutdown() {
        if (this.producer != null) {
            this.producer.shutdown();
        }
    }

    /**
     * 同步发送消息，只要不抛异常就表示成功
     *
     * @param message
     * @return 发送结果，含消息Id
     */
    public SendResult send(Message message) {
        return this.producer.send(message);
    }

    /**
     * 发送消息，Oneway形式，服务器不应答，无法保证消息是否成功到达服务器
     *
     * @param message
     */
    public void sendOneway(Message message) {
        this.producer.sendOneway(message);
    }

    public Properties getProperties() {
        return properties;
    }


    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public boolean isStarted() {
        return this.producer.isStarted();
    }

    public boolean isClosed() {
        return this.producer.isClosed();
    }
}
